package cn.iocoder.yudao.module.biz.controller.admin.resFile;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.biz.controller.admin.resFile.vo.*;
import cn.iocoder.yudao.module.biz.convert.resFile.ResFileConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.resFile.ResFileDO;
import cn.iocoder.yudao.module.biz.service.resFile.ResFileService;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 资源库文件")
@RestController
@RequestMapping("/biz/res-file")
@Validated
public class ResFileController {

    @Resource
    private ResFileService resFileService;

    @PostMapping("/create")
    @ApiOperation("创建资源库文件")
    @PreAuthorize("@ss.hasPermission('biz:res-file:create')")
    public CommonResult<String> createResFile(@Valid @RequestBody ResFileCreateReqVO createReqVO) {
        return success(resFileService.createResFile(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新资源库文件")
    @PreAuthorize("@ss.hasPermission('biz:res-file:update')")
    public CommonResult<Boolean> updateResFile(@Valid @RequestBody ResFileUpdateReqVO updateReqVO) {
        resFileService.updateResFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除资源库文件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:res-file:delete')")
    public CommonResult<Boolean> deleteResFile(@RequestParam("id") String id) {
        resFileService.deleteResFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得资源库文件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:res-file:query')")
    public CommonResult<ResFileRespVO> getResFile(@RequestParam("id") String id) {
        ResFileDO resFile = resFileService.getResFile(id);
        ResFileRespVO resFileRespVO=ResFileConvert.INSTANCE.convert(resFile);

        OssDO ossDO=new OssDO();
        ossDO.setOriginalName(resFile.getFileName());
        ossDO.setUrl(resFile.getFileUrl());
        ossDO.setId(Long.parseLong(resFile.getOssId()));
        List<OssDO> ossDOList = new ArrayList<>();
        ossDOList.add(ossDO);
        resFileRespVO.setOssFile(ossDOList);
        return success(resFileRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得资源库文件列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:res-file:query')")
    public CommonResult<List<ResFileRespVO>> getResFileList(@RequestParam("ids") Collection<String> ids) {
        List<ResFileDO> list = resFileService.getResFileList(ids);
        return success(ResFileConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得资源库文件分页")
    @PreAuthorize("@ss.hasPermission('biz:res-file:query')")
    public CommonResult<PageResult<ResFileRespVO>> getResFilePage(@Valid ResFilePageReqVO pageVO) {
        PageResult<ResFileDO> pageResult = resFileService.getResFilePage(pageVO);
        return success(ResFileConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出资源库文件 Excel")
    @PreAuthorize("@ss.hasPermission('biz:res-file:export')")
    @OperateLog(type = EXPORT)
    public void exportResFileExcel(@Valid ResFileExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ResFileDO> list = resFileService.getResFileList(exportReqVO);
        // 导出 Excel
        List<ResFileExcelVO> datas = ResFileConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "资源库文件.xls", "数据", ResFileExcelVO.class, datas);
    }

}
